﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using System.ComponentModel;
using System.Collections;

namespace AdministrationWeb.Administration
{
    public partial class EditMentor : System.Web.UI.Page
    {
        private const string GET_MENTOR_DETAILS = "SELECT "
                                        + "m.mentor_id, m.pathway_id, m.first_name, m.last_name, m.gender, m.email, m.degree, m.college, m.years_experience_group, "
                                        + "m.job_type, m.location, m.benneficial_course1, m.benneficial_course2, m.benneficial_course3, m.additional_comments, "
                                        + "p.focus_id "
                                        + "FROM mentor m, lkup_pathway p "
                                        + "WHERE m.mentor_id = ?id "
                                        + "AND m.pathway_id = p.pathway_id";

        private const string CONNECTION_STRING_NAME = "ApplicationConnectionString";
        
        protected void Page_Load(object sender, EventArgs e)
        {
            txtFirstName.Focus();
            lblUserId.Text = MySession.Current.userUserID.ToString();
            lblMessage.Text = "";         
        }

        protected void gdvMentors_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            //test for and display error or succes
            if (e.Exception != null)
            {
                lblMessage.Text = "Error Deleting mentor: " + e.Exception.Message;
                e.ExceptionHandled = true;
            }
            else if (e.AffectedRows == 0)
            {
                lblMessage.Text = "Unknown error, mentor not deleted, try again";
            }
            else
            {
                lblMessage.Text = "mentor deleted succesfuly";
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            gdvMentors.DataBind();
            gdvMentors.Visible = true;
            if (gdvMentors.Rows.Count < 1)
            {
                lblError.Visible = true;
                lblError.Text = "No mentors found";
            }
            else
            {
                lblError.Visible = false;
            }
        }

        protected void gdvMentors_SelectedIndexChanged(object sender, EventArgs e)
        {
            getMentorDetails(Convert.ToInt32(gdvMentors.DataKeys[gdvMentors.SelectedIndex].Value.ToString()));
            Response.Redirect("AddMentor.aspx");
                
        }

        public void getMentorDetails(int id)
        {

            //create connection
            MySqlConnection conn = new MySqlConnection(GetConnectionString());
            MySqlCommand command = new MySqlCommand(GET_MENTOR_DETAILS, conn);

            //add parameters
            command.Parameters.AddWithValue("?id", id);

            //connect and run sql
            try
            {
                conn.Open();
                MySqlDataReader reader = command.ExecuteReader();
                reader.Read();

                MySession.Current.mentorFirstName = reader["first_name"].ToString();
                MySession.Current.mentorLastName = reader["last_name"].ToString();
                MySession.Current.mentorGender = reader["gender"].ToString();
                MySession.Current.mentorEmail = reader["email"].ToString();
                MySession.Current.mentorDegree = reader["degree"].ToString();
                MySession.Current.mentorYearsExperience = reader["years_experience_group"].ToString();
                MySession.Current.mentorCollege = reader["college"].ToString();
                MySession.Current.mentorFocusID = reader["focus_id"].ToString();
                MySession.Current.mentorPathwayID = reader["pathway_id"].ToString();
                MySession.Current.mentorAdditionalComments = reader["additional_comments"].ToString();
                MySession.Current.mentorJobType = reader["job_type"].ToString();
                MySession.Current.mentorLocation = reader["location"].ToString();
                MySession.Current.mentorBeneficialCourse1 = reader["benneficial_course1"].ToString();
                MySession.Current.mentorBeneficialCourse2 = reader["benneficial_course2"].ToString();
                MySession.Current.mentorBeneficialCourse3 = reader["benneficial_course3"].ToString();
                MySession.Current.mentorMentorID = reader["mentor_id"].ToString();
            }
            finally
            {
                conn.Close();
            }
                
        }

        private static string GetConnectionString()
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings
            [CONNECTION_STRING_NAME].ConnectionString;
        }
    }
}